/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdminMainPanel.java
 *
 * Created on Aug 25, 2011, 9:00:23 AM
 */
package cw.ui;

import com.jtattoo.plaf.aero.AeroLookAndFeel;
import cw.biz.LevelBiz;
import cw.ulti.Keyboard;
import cw.biz.PuzzleBiz;
import cw.biz.PuzzleSizeBiz;
import cw.biz.QuestionBiz;
import cw.biz.SubjectBiz;
import cw.biz.UserBiz;
import cw.entity.CrossFields;
import cw.entity.Level;
import cw.entity.Puzzle;
import cw.entity.PuzzleSize;
import cw.entity.Question;
import cw.entity.Subject;
import cw.entity.User;
import cw.ulti.CalendarUlti;
import cw.ulti.PuzzleUltil;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;

/**
 *
 * @author VinhTC
 */
public class AdminMainPanel extends javax.swing.JFrame {

    PuzzleBiz puzzlebiz = new PuzzleBiz();
    private static String username = "";
    String keypressed;
    JTextField[][] wo;
    static String[][] cells;
    static String[][] result;
    int posX = -1, posY = -1;
    private static String puzzleName = "";
    private static int subjectId;
    int rowSize = 4;
    int colSize = 4;
    List<Integer> row = new ArrayList<Integer>();
    List<Integer> col = new ArrayList<Integer>();
    List<Integer> rowDisable = new ArrayList<Integer>();
    List<Integer> colDisable = new ArrayList<Integer>();
    PuzzleUltil pul = new PuzzleUltil();
    List<Question> quesList = new ArrayList<Question>();
    String questImport = "";
    int indexPuzzleName;

    /** Creates new form AdminMainPanel */
    public AdminMainPanel() {
        initComponents();
        //Initial puzzle
        InitialGui();
        DisableBeforeChoosePosition();
    }

    /**
     * Constructor for transfer data from Login frame to here
     * @param usn
     */
    public AdminMainPanel(String usn) {
        UserBiz ubiz = new UserBiz();
        User user = ubiz.getUserByUserName(usn);
        CalendarUlti calul = new CalendarUlti();

        initComponents();
        username = usn;
        lblWellcome.setText("Hello: " + username);
        lblLastAccess.setText("Last access: " + user.getLastAccess());
        InitialGui();
        DisableBeforeChoosePosition();

    }

    public static String getPuzzleName() {
        return puzzleName;
    }

    public static void setPuzzleName(String puzzleName) {
        AdminMainPanel.puzzleName = puzzleName;
    }

    public static void setSubjectId(int subjectId) {
        AdminMainPanel.subjectId = subjectId;
    }

    public static int getSubjectId() {
        return subjectId;
    }

    public void addQuestionToQuere(Puzzle puzzle, String question, String answer, boolean direction) {
        Question ques = new Question();
        ques.setPuzzle(puzzle);
        ques.setQuestion(question);
        ques.setAnswer(answer);
        System.out.println("PosX: " + posX + ", posY: " + posY);
        ques.setPositionX(posX);
        ques.setPositionY(posY);
        ques.setDirection(direction);
        ques.setStatus(true);
        quesList.add(ques);
    }

    public void fillPuzzleList() {

        List<String> lst = new ArrayList<String>();
        lst = puzzlebiz.puzzleNameList();

        jcbxPuzzleName.addItem("Select a puzzle");
        for (int i = 0; i < lst.size(); i++) {
            jcbxPuzzleName.addItem(lst.get(i));
        }
    }

    public JTextArea getJtxaQuestion() {
        return jtxaQuestion;
    }

    public void setJtxaQuestion(JTextArea jtxaQuestion) {
        this.jtxaQuestion = jtxaQuestion;
    }

    /**
     * DisableBeforeChoosePosition
     */
    public void DisableBeforeChoosePosition() {
        jcbxDirection.setEnabled(false);
        jbtnLoadQuestion.setEnabled(false);
        jtxaQuestion.setEnabled(false);
        jtxtAnswer.setEnabled(false);
        jbtnInsertQuestion.setEnabled(false);
        jbtnFinishPuzzle.setEnabled(false);
        jbtnResetPuzzle.setEnabled(false);
    }

    /**
     * EnableAfterChoosePosition
     */
    public void EnableAfterChoosePosition() {
        jcbxDirection.setEnabled(true);
        jbtnLoadQuestion.setEnabled(true);
        jtxaQuestion.setEnabled(true);
        jtxtAnswer.setEnabled(true);
        jbtnInsertQuestion.setEnabled(true);
        jbtnFinishPuzzle.setEnabled(true);
        jbtnResetPuzzle.setEnabled(false);
    }

    /**
     * InitialGui
     */
    public void InitialGui() {
        //Initial puzzle
//        generatePuzzle(rowSize, colSize, row, col, rowDisable, colDisable, true);
        fillPuzzleList();
//
//        if (!questImport.trim().isEmpty()) {
//            jtxaQuestion.setText(questImport);
//        }
    }

    /**
     * ShowFrame
     * @param obj
     * @param title
     * @throws SecurityException
     */
    public void ShowFrame(JPanel obj, String title) throws SecurityException {
        JDialog frame = new JDialog();
        frame.setContentPane(obj);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setResizable(false);
        frame.setAlwaysOnTop(true);
        frame.setModal(true);
        frame.setTitle(title);
        frame.setLocationByPlatform(true);
        frame.pack();
        frame.setVisible(true);
    }

    /**
     * ShowFrame
     * @param obj
     * @param title
     * @throws SecurityException
     */
    public void ShowFrameHighScore(JPanel obj, String title) throws SecurityException {
        JDialog frame = new JDialog();
        frame.setContentPane(obj);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setResizable(false);
        frame.setAlwaysOnTop(true);
        frame.setModal(true);
        frame.setTitle(title);
        frame.setLocationByPlatform(true);
        frame.pack();
        frame.setLocation(500, 200);
        frame.setSize(534, 450);
        frame.setVisible(true);
    }

    public void refressPuzzleGui() {
        posX = -1;
        posY = -1;
        generatePuzzle(rowSize, colSize, row, col, rowDisable, colDisable, true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        CrossWordsAdminPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("CrossWordsAdminPU").createEntityManager();
        puzzleQuery = java.beans.Beans.isDesignTime() ? null : CrossWordsAdminPUEntityManager.createQuery("SELECT p FROM Puzzle p");
        puzzleList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : puzzleQuery.getResultList();
        pnlMain = new javax.swing.JPanel();
        jbtnAdminManagement = new javax.swing.JButton();
        jbtnLogout = new javax.swing.JButton();
        jbtnHelp = new javax.swing.JButton();
        jbtnExit = new javax.swing.JButton();
        jbtnSizeManagement = new javax.swing.JButton();
        jbtnSubjectManagement = new javax.swing.JButton();
        jbtnLevelManagement = new javax.swing.JButton();
        jbtnHighScore = new javax.swing.JButton();
        jbtnAbout = new javax.swing.JButton();
        jbtnServerManagement = new javax.swing.JButton();
        jlblVisitWeb = new javax.swing.JLabel();
        lblWellcome = new javax.swing.JLabel();
        lblLastAccess = new javax.swing.JLabel();
        jlblCopyRigh = new javax.swing.JLabel();
        jtbpnPuzzleManagement = new javax.swing.JTabbedPane();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jcbxPuzzleName = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jtxtAnswer = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jcbxDirection = new javax.swing.JComboBox();
        jlblStatus = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtxaQuestion = new javax.swing.JTextArea();
        jbtnInsertQuestion = new javax.swing.JButton();
        jbtnFinishPuzzle = new javax.swing.JButton();
        jbtnShowDetaisOfPuzzle = new javax.swing.JButton();
        jbtnLoadQuestion = new javax.swing.JButton();
        jbtnResetPuzzle = new javax.swing.JButton();
        jpnPuzzle = new javax.swing.JPanel();
        jspnPuzzleInterface = new javax.swing.JScrollPane();
        jpnPuzzleInterface = new javax.swing.JPanel();
        jbtnChoosePosition = new javax.swing.JButton();
        jpnPuzzleManagement = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtblPuzzleList = new javax.swing.JTable();
        jLabel5 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jtxtPuzzleName = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        cboGameSize = new javax.swing.JComboBox();
        cboSubject = new javax.swing.JComboBox();
        cboLevel = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jbtnCreatePuzzle = new javax.swing.JButton();
        jbtnUpdatePuzzle = new javax.swing.JButton();
        jbtnDeletePuzzle = new javax.swing.JButton();
        jcbStatus = new javax.swing.JCheckBox();
        jbtnShowQuestionOfPuzzle = new javax.swing.JButton();
        jlblCreatedStatus = new javax.swing.JLabel();
        lblbackground = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("CrossWords - Admin main panel");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setName("AdminMainPanel"); // NOI18N
        setResizable(false);

        pnlMain.setPreferredSize(new java.awt.Dimension(950, 720));
        pnlMain.setLayout(null);

        jbtnAdminManagement.setFont(new java.awt.Font("Tahoma", 0, 14));
        jbtnAdminManagement.setForeground(new java.awt.Color(102, 0, 0));
        jbtnAdminManagement.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/Register.png"))); // NOI18N
        jbtnAdminManagement.setText("Admin");
        jbtnAdminManagement.setToolTipText("Administrator management");
        jbtnAdminManagement.setBorderPainted(false);
        jbtnAdminManagement.setContentAreaFilled(false);
        jbtnAdminManagement.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jbtnAdminManagement.setMaximumSize(new java.awt.Dimension(55, 16));
        jbtnAdminManagement.setMinimumSize(new java.awt.Dimension(55, 16));
        jbtnAdminManagement.setPreferredSize(new java.awt.Dimension(55, 16));
        jbtnAdminManagement.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnAdminManagementActionPerformed(evt);
            }
        });
        pnlMain.add(jbtnAdminManagement);
        jbtnAdminManagement.setBounds(460, 70, 110, 40);

        jbtnLogout.setFont(new java.awt.Font("Tahoma", 0, 14));
        jbtnLogout.setForeground(new java.awt.Color(102, 0, 0));
        jbtnLogout.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/logoutSystem.gif"))); // NOI18N
        jbtnLogout.setText(" Logout");
        jbtnLogout.setToolTipText("Click here to logout");
        jbtnLogout.setBorderPainted(false);
        jbtnLogout.setContentAreaFilled(false);
        jbtnLogout.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jbtnLogout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnLogoutActionPerformed(evt);
            }
        });
        pnlMain.add(jbtnLogout);
        jbtnLogout.setBounds(570, 69, 130, 41);

        jbtnHelp.setFont(new java.awt.Font("Tahoma", 0, 14));
        jbtnHelp.setForeground(new java.awt.Color(102, 0, 0));
        jbtnHelp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/help_32.png"))); // NOI18N
        jbtnHelp.setText(" Help");
        jbtnHelp.setToolTipText("Click here to get helped");
        jbtnHelp.setBorderPainted(false);
        jbtnHelp.setContentAreaFilled(false);
        jbtnHelp.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jbtnHelp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnHelpActionPerformed(evt);
            }
        });
        pnlMain.add(jbtnHelp);
        jbtnHelp.setBounds(700, 70, 110, 40);

        jbtnExit.setFont(new java.awt.Font("Tahoma", 0, 14));
        jbtnExit.setForeground(new java.awt.Color(102, 0, 0));
        jbtnExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/close_32_1.png"))); // NOI18N
        jbtnExit.setText(" Exit");
        jbtnExit.setToolTipText("Click here to exit");
        jbtnExit.setBorderPainted(false);
        jbtnExit.setContentAreaFilled(false);
        jbtnExit.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jbtnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnExitActionPerformed(evt);
            }
        });
        pnlMain.add(jbtnExit);
        jbtnExit.setBounds(810, 69, 110, 41);

        jbtnSizeManagement.setFont(new java.awt.Font("Tahoma", 2, 12));
        jbtnSizeManagement.setForeground(new java.awt.Color(102, 0, 0));
        jbtnSizeManagement.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/calculate.png"))); // NOI18N
        jbtnSizeManagement.setText("Puzzle size");
        jbtnSizeManagement.setToolTipText("Click here to puzzle size management");
        jbtnSizeManagement.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jbtnSizeManagement.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jbtnSizeManagement.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnSizeManagementActionPerformed(evt);
            }
        });
        pnlMain.add(jbtnSizeManagement);
        jbtnSizeManagement.setBounds(40, 260, 130, 30);

        jbtnSubjectManagement.setFont(new java.awt.Font("Tahoma", 2, 12));
        jbtnSubjectManagement.setForeground(new java.awt.Color(102, 0, 0));
        jbtnSubjectManagement.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/script_edit.png"))); // NOI18N
        jbtnSubjectManagement.setText("Subject");
        jbtnSubjectManagement.setToolTipText("Click here to subject management");
        jbtnSubjectManagement.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jbtnSubjectManagement.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jbtnSubjectManagement.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnSubjectManagementActionPerformed(evt);
            }
        });
        pnlMain.add(jbtnSubjectManagement);
        jbtnSubjectManagement.setBounds(40, 180, 130, 30);

        jbtnLevelManagement.setFont(new java.awt.Font("Tahoma", 2, 12));
        jbtnLevelManagement.setForeground(new java.awt.Color(102, 0, 0));
        jbtnLevelManagement.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/view_text.png"))); // NOI18N
        jbtnLevelManagement.setText("Level");
        jbtnLevelManagement.setToolTipText("Click here to puzzle level management");
        jbtnLevelManagement.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jbtnLevelManagement.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jbtnLevelManagement.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnLevelManagementActionPerformed(evt);
            }
        });
        pnlMain.add(jbtnLevelManagement);
        jbtnLevelManagement.setBounds(40, 220, 130, 30);

        jbtnHighScore.setFont(new java.awt.Font("Tahoma", 2, 12));
        jbtnHighScore.setForeground(new java.awt.Color(102, 0, 0));
        jbtnHighScore.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/hightscore.png"))); // NOI18N
        jbtnHighScore.setText("High Score");
        jbtnHighScore.setToolTipText("Click here to puzzle size management");
        jbtnHighScore.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jbtnHighScore.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jbtnHighScore.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnHighScoreActionPerformed(evt);
            }
        });
        pnlMain.add(jbtnHighScore);
        jbtnHighScore.setBounds(40, 300, 130, 30);

        jbtnAbout.setFont(new java.awt.Font("Tahoma", 2, 12));
        jbtnAbout.setForeground(new java.awt.Color(102, 0, 0));
        jbtnAbout.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/info-16.png"))); // NOI18N
        jbtnAbout.setText("About us");
        jbtnAbout.setToolTipText("Some informations bout us");
        jbtnAbout.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jbtnAbout.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jbtnAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnAboutActionPerformed(evt);
            }
        });
        pnlMain.add(jbtnAbout);
        jbtnAbout.setBounds(40, 350, 130, 30);

        jbtnServerManagement.setFont(new java.awt.Font("Tahoma", 2, 11));
        jbtnServerManagement.setForeground(new java.awt.Color(0, 0, 255));
        jbtnServerManagement.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/profile.png"))); // NOI18N
        jbtnServerManagement.setText("Server Mng");
        jbtnServerManagement.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jbtnServerManagement.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnServerManagementActionPerformed(evt);
            }
        });
        pnlMain.add(jbtnServerManagement);
        jbtnServerManagement.setBounds(40, 393, 130, 30);

        jlblVisitWeb.setToolTipText("Click here to visit our website");
        jlblVisitWeb.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jlblVisitWeb.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jlblVisitWebMouseClicked(evt);
            }
        });
        pnlMain.add(jlblVisitWeb);
        jlblVisitWeb.setBounds(30, 460, 150, 150);

        lblWellcome.setForeground(new java.awt.Color(0, 102, 204));
        lblWellcome.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/profile.png"))); // NOI18N
        lblWellcome.setText(" Hello:Bao Djnh");
        lblWellcome.setToolTipText("We're welcome!");
        pnlMain.add(lblWellcome);
        lblWellcome.setBounds(390, 10, 240, 30);

        lblLastAccess.setForeground(new java.awt.Color(0, 102, 204));
        lblLastAccess.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/calendar_empty.png"))); // NOI18N
        lblLastAccess.setText(" Last accsess:20/8");
        lblLastAccess.setToolTipText("This is last access date time of you");
        pnlMain.add(lblLastAccess);
        lblLastAccess.setBounds(640, 10, 290, 30);

        jlblCopyRigh.setForeground(new java.awt.Color(153, 153, 153));
        jlblCopyRigh.setText("CrossWords Game-Design by Dragon Group @ 2011");
        jlblCopyRigh.setToolTipText("Click here to view product copyrigh @");
        jlblCopyRigh.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jlblCopyRigh.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jlblCopyRighMouseClicked(evt);
            }
        });
        pnlMain.add(jlblCopyRigh);
        jlblCopyRigh.setBounds(80, 660, 340, 14);

        jtbpnPuzzleManagement.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jtbpnPuzzleManagement.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jtbpnPuzzleManagementStateChanged(evt);
            }
        });

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Setting puzzle"));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Puzzle: ");

        jcbxPuzzleName.setToolTipText("Choose a puzzle name");
        jcbxPuzzleName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbxPuzzleNameActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Question: ");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("Answer: ");

        jtxtAnswer.setToolTipText("Lets enter anwser of question");
        jtxtAnswer.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jtxtAnswerKeyReleased(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel4.setText("Direction: ");

        jcbxDirection.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Horizontal", "Vertical" }));
        jcbxDirection.setToolTipText("Lest choose direction of question");

        jScrollPane1.setPreferredSize(new java.awt.Dimension(66, 66));

        jtxaQuestion.setColumns(20);
        jtxaQuestion.setRows(2);
        jtxaQuestion.setTabSize(4);
        jtxaQuestion.setToolTipText("Lets enter question content of this position");
        jScrollPane1.setViewportView(jtxaQuestion);

        jbtnInsertQuestion.setForeground(new java.awt.Color(102, 0, 0));
        jbtnInsertQuestion.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/45.png"))); // NOI18N
        jbtnInsertQuestion.setText("Insert Question");
        jbtnInsertQuestion.setToolTipText("Click here to insert question for this position");
        jbtnInsertQuestion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnInsertQuestionActionPerformed(evt);
            }
        });

        jbtnFinishPuzzle.setForeground(new java.awt.Color(102, 0, 0));
        jbtnFinishPuzzle.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/basket_put.png"))); // NOI18N
        jbtnFinishPuzzle.setText("Finish Puzzle");
        jbtnFinishPuzzle.setToolTipText("Click here to finishs puzzle");
        jbtnFinishPuzzle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnFinishPuzzleActionPerformed(evt);
            }
        });

        jbtnShowDetaisOfPuzzle.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/info-16.png"))); // NOI18N
        jbtnShowDetaisOfPuzzle.setText("Show details of puzzle");
        jbtnShowDetaisOfPuzzle.setToolTipText("Click here to show detais of puzzle");
        jbtnShowDetaisOfPuzzle.setBorderPainted(false);
        jbtnShowDetaisOfPuzzle.setContentAreaFilled(false);
        jbtnShowDetaisOfPuzzle.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jbtnShowDetaisOfPuzzle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnShowDetaisOfPuzzleActionPerformed(evt);
            }
        });

        jbtnLoadQuestion.setVisible(false);
        jbtnLoadQuestion.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/mail_replyall.png"))); // NOI18N
        jbtnLoadQuestion.setText("Load question");
        jbtnLoadQuestion.setToolTipText("Click here to load question from store");
        jbtnLoadQuestion.setBorderPainted(false);
        jbtnLoadQuestion.setContentAreaFilled(false);
        jbtnLoadQuestion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnLoadQuestionActionPerformed(evt);
            }
        });

        jbtnResetPuzzle.setForeground(new java.awt.Color(102, 0, 0));
        jbtnResetPuzzle.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/cancel.png"))); // NOI18N
        jbtnResetPuzzle.setText("Reset Puzzle");
        jbtnResetPuzzle.setToolTipText("Click here to insert question for this position");
        jbtnResetPuzzle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnResetPuzzleActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jcbxPuzzleName, 0, 157, Short.MAX_VALUE))
                    .addComponent(jlblStatus)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jbtnShowDetaisOfPuzzle, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jcbxDirection, 0, 142, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtxtAnswer, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)
                            .addGroup(jPanel4Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jbtnLoadQuestion))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)
                            .addComponent(jLabel3)))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(45, 45, 45)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jbtnResetPuzzle, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jbtnInsertQuestion, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jbtnFinishPuzzle, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jcbxPuzzleName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jbtnShowDetaisOfPuzzle)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jcbxDirection, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jbtnLoadQuestion))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addGap(4, 4, 4)
                .addComponent(jtxtAnswer, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jbtnInsertQuestion, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jlblStatus)
                    .addComponent(jbtnResetPuzzle, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jbtnFinishPuzzle, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
                .addContainerGap())
        );

        jpnPuzzle.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder("Puzzle interface")));
        jpnPuzzle.setAutoscrolls(true);

        jspnPuzzleInterface.setPreferredSize(new java.awt.Dimension(500, 400));

        jpnPuzzleInterface.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jpnPuzzleInterface.setOpaque(false);
        jpnPuzzleInterface.setPreferredSize(new java.awt.Dimension(400, 340));

        javax.swing.GroupLayout jpnPuzzleInterfaceLayout = new javax.swing.GroupLayout(jpnPuzzleInterface);
        jpnPuzzleInterface.setLayout(jpnPuzzleInterfaceLayout);
        jpnPuzzleInterfaceLayout.setHorizontalGroup(
            jpnPuzzleInterfaceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 432, Short.MAX_VALUE)
        );
        jpnPuzzleInterfaceLayout.setVerticalGroup(
            jpnPuzzleInterfaceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 357, Short.MAX_VALUE)
        );

        jspnPuzzleInterface.setViewportView(jpnPuzzleInterface);

        jbtnChoosePosition.setForeground(new java.awt.Color(102, 0, 0));
        jbtnChoosePosition.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/login.png"))); // NOI18N
        jbtnChoosePosition.setText("Choose position");
        jbtnChoosePosition.setToolTipText("Click here to choose position of question");
        jbtnChoosePosition.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnChoosePositionActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jpnPuzzleLayout = new javax.swing.GroupLayout(jpnPuzzle);
        jpnPuzzle.setLayout(jpnPuzzleLayout);
        jpnPuzzleLayout.setHorizontalGroup(
            jpnPuzzleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnPuzzleLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jspnPuzzleInterface, javax.swing.GroupLayout.PREFERRED_SIZE, 434, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(jpnPuzzleLayout.createSequentialGroup()
                .addGap(151, 151, 151)
                .addComponent(jbtnChoosePosition, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(145, Short.MAX_VALUE))
        );
        jpnPuzzleLayout.setVerticalGroup(
            jpnPuzzleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnPuzzleLayout.createSequentialGroup()
                .addComponent(jspnPuzzleInterface, javax.swing.GroupLayout.PREFERRED_SIZE, 359, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jbtnChoosePosition, javax.swing.GroupLayout.PREFERRED_SIZE, 28, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpnPuzzle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jpnPuzzle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        jtbpnPuzzleManagement.addTab("Setting Puzzle", new javax.swing.ImageIcon(getClass().getResource("/image/add.png")), jPanel3); // NOI18N

        jpnPuzzleManagement.setAutoscrolls(true);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Puzzle list"));
        jPanel1.setAutoscrolls(true);

        jtblPuzzleList.setAutoCreateRowSorter(true);
        jtblPuzzleList.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, puzzleList, jtblPuzzleList);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${puzzleName}"));
        columnBinding.setColumnName("Puzzle Name");
        columnBinding.setColumnClass(String.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jtblPuzzleList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jtblPuzzleListMouseReleased(evt);
            }
        });
        jScrollPane2.setViewportView(jtblPuzzleList);

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/cross.png"))); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 379, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 86, Short.MAX_VALUE)
                .addComponent(jLabel5)
                .addGap(60, 60, 60))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)
                .addGap(11, 11, 11))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 145, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Update puzzle"));
        jPanel2.setAutoscrolls(true);

        jLabel6.setText("Puzzle name:");

        jLabel7.setText("Size:");

        jLabel8.setText("Subject:");

        jLabel9.setText("Level:");

        try {
            PuzzleSizeBiz PuzzleSizeBiz=new PuzzleSizeBiz();
            List<PuzzleSize> lstPuzzleSize=new ArrayList<PuzzleSize>();
            lstPuzzleSize=PuzzleSizeBiz.getAllPuzzleSize();
            for (int i = 0; i < lstPuzzleSize.size(); i++) {
                cboGameSize.addItem(lstPuzzleSize.get(i).getSizeName());
            }
        } catch (Exception e) {}

        try {
            SubjectBiz subjectBiz=new SubjectBiz();
            List<Subject> lstSubject=new ArrayList<Subject>();
            lstSubject=subjectBiz.getAllSubject();
            for (int i = 0; i < lstSubject.size(); i++) {
                cboSubject.addItem(lstSubject.get(i).getSbjName());
            }
        } catch (Exception e) {}

        try {
            LevelBiz levelBiz=new LevelBiz();
            List<cw.entity.Level> lstLevel=new ArrayList<cw.entity.Level>();
            lstLevel=levelBiz.getAllLevel();
            for (int i = 0; i < lstLevel.size(); i++) {
                cboLevel.addItem(lstLevel.get(i).getLevelName());
            }
        } catch (Exception e) {}

        jLabel10.setText("Created:");

        jLabel11.setText("Status:");

        jbtnCreatePuzzle.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/add.png"))); // NOI18N
        jbtnCreatePuzzle.setText("Create");
        jbtnCreatePuzzle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCreatePuzzleActionPerformed(evt);
            }
        });

        jbtnUpdatePuzzle.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/45.png"))); // NOI18N
        jbtnUpdatePuzzle.setText("Update");
        jbtnUpdatePuzzle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnUpdatePuzzleActionPerformed(evt);
            }
        });

        jbtnDeletePuzzle.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/cancel_1.png"))); // NOI18N
        jbtnDeletePuzzle.setText("Delete");
        jbtnDeletePuzzle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnDeletePuzzleActionPerformed(evt);
            }
        });

        jcbStatus.setText("Enable");

        jbtnShowQuestionOfPuzzle.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/info-16.png"))); // NOI18N
        jbtnShowQuestionOfPuzzle.setText("Show questions");
        jbtnShowQuestionOfPuzzle.setBorderPainted(false);
        jbtnShowQuestionOfPuzzle.setContentAreaFilled(false);
        jbtnShowQuestionOfPuzzle.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jbtnShowQuestionOfPuzzle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnShowQuestionOfPuzzleActionPerformed(evt);
            }
        });

        jlblCreatedStatus.setText("True");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(jLabel11)
                    .addComponent(jLabel9)
                    .addComponent(jLabel10))
                .addGap(4, 4, 4)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cboGameSize, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jtxtPuzzleName, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(24, 24, 24)
                                .addComponent(jbtnShowQuestionOfPuzzle))
                            .addComponent(jcbStatus)
                            .addComponent(cboSubject, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cboLevel, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 57, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jbtnCreatePuzzle, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jbtnUpdatePuzzle, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jbtnDeletePuzzle, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(77, 77, 77))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jlblCreatedStatus)
                        .addContainerGap())))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jbtnShowQuestionOfPuzzle)
                    .addComponent(jtxtPuzzleName, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(cboGameSize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(cboSubject, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cboLevel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jlblCreatedStatus))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(jcbStatus))
                .addContainerGap(23, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(59, Short.MAX_VALUE)
                .addComponent(jbtnCreatePuzzle)
                .addGap(18, 18, 18)
                .addComponent(jbtnUpdatePuzzle)
                .addGap(18, 18, 18)
                .addComponent(jbtnDeletePuzzle)
                .addGap(27, 27, 27))
        );

        javax.swing.GroupLayout jpnPuzzleManagementLayout = new javax.swing.GroupLayout(jpnPuzzleManagement);
        jpnPuzzleManagement.setLayout(jpnPuzzleManagementLayout);
        jpnPuzzleManagementLayout.setHorizontalGroup(
            jpnPuzzleManagementLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpnPuzzleManagementLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpnPuzzleManagementLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jpnPuzzleManagementLayout.setVerticalGroup(
            jpnPuzzleManagementLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnPuzzleManagementLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jtbpnPuzzleManagement.addTab("Puzzle management", new javax.swing.ImageIcon(getClass().getResource("/image/script_edit.png")), jpnPuzzleManagement); // NOI18N

        pnlMain.add(jtbpnPuzzleManagement);
        jtbpnPuzzleManagement.setBounds(190, 180, 730, 470);

        lblbackground.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/adminBackground.png"))); // NOI18N
        pnlMain.add(lblbackground);
        lblbackground.setBounds(0, 0, 950, 680);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlMain, javax.swing.GroupLayout.DEFAULT_SIZE, 950, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlMain, javax.swing.GroupLayout.PREFERRED_SIZE, 680, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        bindingGroup.bind();

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnSizeManagementActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnSizeManagementActionPerformed
        ShowFrame(new SizeManagement(), "Cross Words - Puzzle size management");
    }//GEN-LAST:event_jbtnSizeManagementActionPerformed

    private void jbtnSubjectManagementActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnSubjectManagementActionPerformed
        ShowFrame(new SubjectManagement(), "Cross Words - Subject management");
    }//GEN-LAST:event_jbtnSubjectManagementActionPerformed

    private void jbtnLevelManagementActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnLevelManagementActionPerformed
        ShowFrame(new LevelManagement(), "Cross Words - Level management");
    }//GEN-LAST:event_jbtnLevelManagementActionPerformed

    private void jbtnLogoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnLogoutActionPerformed
        int index = JOptionPane.showConfirmDialog(jbtnInsertQuestion, "Are you sure want to logout?", "Confirm for logout", JOptionPane.YES_NO_OPTION);
        if (index == 0) {
            Login login = new Login();
            login.setResizable(false);
            login.setVisible(true);
            this.dispose();
        }
    }//GEN-LAST:event_jbtnLogoutActionPerformed

    private void jbtnAdminManagementActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAdminManagementActionPerformed
        ShowFrame(new AdminManagement(), "Cross Words - Admin management");
    }//GEN-LAST:event_jbtnAdminManagementActionPerformed

    private void jbtnAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAboutActionPerformed
        ShowFrame(new About(), "Cross Words - About us");
    }//GEN-LAST:event_jbtnAboutActionPerformed

    private void jbtnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnExitActionPerformed
        int index = JOptionPane.showConfirmDialog(jbtnExit, "Are you sure want to exit?", "Confirm for exit", JOptionPane.YES_NO_OPTION);
        if (index == 0) {
            System.exit(0);
        }
    }//GEN-LAST:event_jbtnExitActionPerformed

    private void jcbxPuzzleNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbxPuzzleNameActionPerformed
        Puzzle puzzle = new Puzzle();
        if (jcbxPuzzleName.getSelectedIndex() == 0) {
        } else {
            indexPuzzleName = jcbxPuzzleName.getSelectedIndex();
            puzzle = puzzlebiz.findPuzzleByName(jcbxPuzzleName.getSelectedItem().toString());
            rowSize = puzzle.getPuzzleSize().getRowSize();
            colSize = puzzle.getPuzzleSize().getColSize();
            cells = new String[rowSize][colSize];
            System.out.println("cells length: " + cells.length);
            generatePuzzle(rowSize, colSize, row, col, rowDisable, colDisable, true);
        }

    }//GEN-LAST:event_jcbxPuzzleNameActionPerformed

    private void jlblVisitWebMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jlblVisitWebMouseClicked
        PuzzleUltil pul = new PuzzleUltil();
        pul.openURL("http://www.crossword-puzzles.co.uk/");
    }//GEN-LAST:event_jlblVisitWebMouseClicked

    private void jbtnHighScoreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnHighScoreActionPerformed
        ShowFrameHighScore(new HighScores(), "Cross Words - High Score");
    }//GEN-LAST:event_jbtnHighScoreActionPerformed

    private void jlblCopyRighMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jlblCopyRighMouseClicked
        ShowFrame(new CopyRigh(), "Cross Words - @copyrigh");
    }//GEN-LAST:event_jlblCopyRighMouseClicked

    private void jbtnChoosePositionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnChoosePositionActionPerformed
        if (posX == -1 || posY == -1) {
            JOptionPane.showMessageDialog(this, "Lets choose a position");
        } else {
            EnableAfterChoosePosition();
            generatePuzzle(rowSize, colSize, row, col, rowDisable, colDisable, false);
        }

    }//GEN-LAST:event_jbtnChoosePositionActionPerformed

    private void jbtnInsertQuestionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnInsertQuestionActionPerformed
        System.out.println("PosX: " + posX + ", posY: " + posY);
        DisableBeforeChoosePosition();
        Puzzle puzzle = new Puzzle();
        puzzle = puzzlebiz.findPuzzleByName(jcbxPuzzleName.getSelectedItem().toString());
        jbtnFinishPuzzle.setEnabled(true);
        jbtnResetPuzzle.setEnabled(true);
        String answer = jtxtAnswer.getText().trim();
        String question = jtxaQuestion.getText().trim();
        boolean direction = pul.getDirection(jcbxDirection.getSelectedIndex());
        if (!answer.trim().isEmpty() && !question.isEmpty()) {
            char[][] answerCells = pul.getCellsByPosisitonAndDirection(rowSize, colSize, posX, posY, answer, direction);
            if (pul.checkCharArr(answerCells)) {
                if (!pul.checkCharExist(answerCells, cells)) {
                    cells = pul.getCellsToStringArr(answerCells, cells);

                    addQuestionToQuere(puzzle, question, answer, direction);
                    refressPuzzleGui();
                } else {
                    int index = JOptionPane.showConfirmDialog(jbtnInsertQuestion, "Has some cell is not empty, are you sure want to add this question?", "Confirm for add question", JOptionPane.YES_NO_OPTION);
                    if (index == 0) {
                        cells = pul.getCellsToStringArr(answerCells, cells);
                        addQuestionToQuere(puzzle, question, answer, direction);
                        refressPuzzleGui();
                    } else {
                        refressPuzzleGui();
                    }
                }
            } else {
                System.out.println("Answer Empty");
            }
        } else {
            JOptionPane.showMessageDialog(this, "Lets enter question and answer! Please choose position again!");
        }
        refressPuzzleGui();
    }//GEN-LAST:event_jbtnInsertQuestionActionPerformed

    private void jbtnFinishPuzzleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnFinishPuzzleActionPerformed
        QuestionBiz quesBiz = new QuestionBiz();
        Puzzle puzzle = new Puzzle();
        puzzle = puzzlebiz.findPuzzleByName(jcbxPuzzleName.getSelectedItem().toString());
        puzzle.setCreated(true);
        if (quesBiz.addQuestion(quesList, puzzle)) {
            JOptionPane.showMessageDialog(this, "Successfully!!! Puzzle has created");

            jcbxPuzzleName.removeItemAt(indexPuzzleName);
        } else {
            JOptionPane.showMessageDialog(this, "Error!!! Create Puzzle failured!");
        }
        DisableBeforeChoosePosition();
        posX = -1;
        posY = -1;
        keypressed = "";
        cells = new String[rowSize][colSize];
        quesList = new ArrayList<Question>();
        generatePuzzle(rowSize, colSize, row, col, rowDisable, colDisable, true);
    }//GEN-LAST:event_jbtnFinishPuzzleActionPerformed

    private void jbtnShowDetaisOfPuzzleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnShowDetaisOfPuzzleActionPerformed
        puzzleName = jcbxPuzzleName.getSelectedItem().toString();

        ShowFrame(new PuzzleDetails(), "Cross Words - Puzzle details");
    }//GEN-LAST:event_jbtnShowDetaisOfPuzzleActionPerformed

//    public cw.ulti.ConnectDialog dialog = null;
    private void jbtnLoadQuestionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnLoadQuestionActionPerformed
//        Puzzle puzzle = new Puzzle();
//        puzzle = puzzlebiz.findPuzzleByName(jcbxPuzzleName.getSelectedItem().toString());
//        subjectId = puzzle.getSubject().getSubjectId();
//
////        QuestionList quests = null;
////        quests = new QuestionList(this);
////
////        if (quests.showDialog(questImport)) {
////            jtxaQuestion.setText(questImport);
////            validate();
////        }
//        dialog = new cw.ulti.ConnectDialog(this);
//        if (dialog.showDialog(questImport)) {
////            jtxaQuestion.setText(questImport);
////            validate();
//        }
    }//GEN-LAST:event_jbtnLoadQuestionActionPerformed

    private void jbtnResetPuzzleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnResetPuzzleActionPerformed
        cells = new String[rowSize][colSize];
        keypressed = "";
        quesList = new ArrayList<Question>();
        generatePuzzle(rowSize, colSize, row, col, rowDisable, colDisable, true);
    }//GEN-LAST:event_jbtnResetPuzzleActionPerformed

    private void jtxtAnswerKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtxtAnswerKeyReleased
        //do something
    }//GEN-LAST:event_jtxtAnswerKeyReleased

    private void jtblPuzzleListMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtblPuzzleListMouseReleased
        jbtnCreatePuzzle.setText("Create");
        Puzzle puzzle = new Puzzle();
        ListSelectionModel selectionModel = jtblPuzzleList.getSelectionModel();
        String puzzName = jtblPuzzleList.getValueAt(jtblPuzzleList.getSelectedRow(), jtblPuzzleList.getSelectedColumn()).toString();

        puzzle = puzzlebiz.findPuzzleByName(puzzName);
        cboGameSize.setSelectedItem(puzzle.getPuzzleSize().getSizeName());
        cboLevel.setSelectedItem(puzzle.getLevel().getLevelName());
        cboSubject.setSelectedItem(puzzle.getSubject().getSbjName());
        if (puzzle.getCreated()) {
            jlblCreatedStatus.setText("True");

        } else {
            jlblCreatedStatus.setText("False");
        }
        if (puzzle.getStatus()) {
            jcbStatus.setSelected(true);
        } else {
            jcbStatus.setSelected(false);
        }
        jtxtPuzzleName.setText(puzzName);
        jtxtPuzzleName.setEditable(false);
    }//GEN-LAST:event_jtblPuzzleListMouseReleased

    private void jbtnShowQuestionOfPuzzleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnShowQuestionOfPuzzleActionPerformed
        Puzzle puzzle = new Puzzle();

        puzzle.setPuzzleName(jtxtPuzzleName.getText().trim());
        try {
            puzzle = puzzlebiz.findPuzzleByName(jtxtPuzzleName.getText().trim());
        } catch (Exception e) {
            Logger.getLogger(AdminMainPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, e);
        }

        ShowFrame(new QuestionManagement(puzzle.getPuzzleId()), "Cross Words - Question list of puzzle");

    }//GEN-LAST:event_jbtnShowQuestionOfPuzzleActionPerformed

    private void jtbpnPuzzleManagementStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jtbpnPuzzleManagementStateChanged
        if (jtbpnPuzzleManagement.getSelectedIndex() == 1) {
        }
    }//GEN-LAST:event_jtbpnPuzzleManagementStateChanged

    private void jbtnServerManagementActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnServerManagementActionPerformed
        ServerService server = new ServerService();

        server.setVisible(true);
    }//GEN-LAST:event_jbtnServerManagementActionPerformed
    private void jbtnUpdatePuzzleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnUpdatePuzzleActionPerformed
        // Update puzzle
        int index = JOptionPane.showConfirmDialog(jbtnExit, "Are you sure want to update puzzle?", "Confirm for update puzzle", JOptionPane.YES_NO_OPTION);


        if (index == 0) {
            Puzzle puzzle = new Puzzle();

            puzzle.setPuzzleName(jtxtPuzzleName.getText().trim());

            try {
                puzzle = puzzlebiz.findPuzzleByName(jtxtPuzzleName.getText().trim());

                if (puzzle != null) {
                    if (jcbStatus.isSelected()) {
                        puzzle.setStatus(true);
                    } else {
                        puzzle.setStatus(false);
                    }
                    Level leve = new Level();
                    LevelBiz leveBiz = new LevelBiz();
                    leve = leveBiz.getLevelByName(cboLevel.getSelectedItem().toString());
                    puzzle.setLevel(leve);

                    PuzzleSize size = new PuzzleSize();
                    PuzzleSizeBiz sizeBiz = new PuzzleSizeBiz();
                    size = sizeBiz.getSizeByName(cboGameSize.getSelectedItem().toString());
                    puzzle.setPuzzleSize(size);

                    Subject sbj = new Subject();
                    SubjectBiz sbjBiz = new SubjectBiz();
                    sbj = sbjBiz.getSubjectByName(cboSubject.getSelectedItem().toString());
                    puzzle.setSubject(sbj);

                    if (puzzlebiz.updatePuzzle(puzzle)) {
                        JOptionPane.showMessageDialog(this, "Successful...! This puzzle is updated");
                    } else {
                        JOptionPane.showMessageDialog(this, "Error...! This puzzle cannot updated");
                    }
                } else {
                    JOptionPane.showMessageDialog(this, "Error...! This puzzle not exist in system");
                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "Error...! This puzzle cannot updated.");
                Logger.getLogger(AdminMainPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, e);
            }
        }
    }//GEN-LAST:event_jbtnUpdatePuzzleActionPerformed

    private void jbtnCreatePuzzleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnCreatePuzzleActionPerformed
        // Update puzzle
        try {
            jtxtPuzzleName.setEditable(true);
            if (jbtnCreatePuzzle.getText().equalsIgnoreCase("Add")) {
                if (puzzlebiz.checkPuzzleNotExist(jtxtPuzzleName.getText().trim())) {
                    Puzzle puzzle = new Puzzle();
                    puzzle.setPuzzleName(jtxtPuzzleName.getText());
                    if (jcbStatus.isSelected()) {
                        puzzle.setStatus(true);
                    } else {
                        puzzle.setStatus(false);
                    }
                    Level leve = new Level();
                    LevelBiz leveBiz = new LevelBiz();
                    leve = leveBiz.getLevelByName(cboLevel.getSelectedItem().toString());
                    puzzle.setLevel(leve);

                    PuzzleSize size = new PuzzleSize();
                    PuzzleSizeBiz sizeBiz = new PuzzleSizeBiz();
                    size = sizeBiz.getSizeByName(cboGameSize.getSelectedItem().toString());
                    puzzle.setPuzzleSize(size);

                    Subject sbj = new Subject();
                    SubjectBiz sbjBiz = new SubjectBiz();
                    sbj = sbjBiz.getSubjectByName(cboSubject.getSelectedItem().toString());
                    puzzle.setSubject(sbj);

                    if (puzzlebiz.createPuzzle(puzzle)) {
                        jbtnCreatePuzzle.setText("Create");
                        JOptionPane.showMessageDialog(this, "Successful...! This puzzle is created");
                    } else {
                        JOptionPane.showMessageDialog(this, "Error...! This puzzle cannot created");
                    }
                } else {
                    JOptionPane.showMessageDialog(this, "Error...! This puzzle existed");
                }
            } else {
                jbtnCreatePuzzle.setText("Add");
            }

        } catch (Exception e) {
            Logger.getLogger(AdminMainPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, e);
        }
    }//GEN-LAST:event_jbtnCreatePuzzleActionPerformed
    private void jbtnDeletePuzzleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnDeletePuzzleActionPerformed
        int index = JOptionPane.showConfirmDialog(jbtnExit, "Are you sure want to delete puzzle?", "Confirm for delete puzzle", JOptionPane.YES_NO_OPTION);
        if (index == 0) {
            Puzzle puzzle = new Puzzle();
            try {
                puzzle = puzzlebiz.findPuzzleByName(jtxtPuzzleName.getText());

                if (puzzlebiz.deletePuzzle(puzzle)) {
                    JOptionPane.showMessageDialog(this, "Successful...! This puzzle is deleted");
                    jtblPuzzleList.updateUI();
                } else {
                    JOptionPane.showMessageDialog(this, "Error...! This puzzle cannot deleted");
                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "Error...! This puzzle cannot deleted.");
                Logger.getLogger(AdminMainPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, e);
            }
        }
    }//GEN-LAST:event_jbtnDeletePuzzleActionPerformed

    private void jbtnHelpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnHelpActionPerformed
        try {
            Desktop.getDesktop().open(new File("src/Docs/UserGuide.chm"));
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "User guide is not available");
        }
    }//GEN-LAST:event_jbtnHelpActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    UIManager.setLookAndFeel(new AeroLookAndFeel());
                    AdminMainPanel amp = new AdminMainPanel();
                    amp.setVisible(true);
                } catch (Exception e) {
                    Logger.getLogger(AdminMainPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, e);
                }
            }
        });
    }

    /**
     * generatePuzzle to generate puzzle interface
     * @param rowSize
     * @param colSize
     * @param row - is row position of character will change background color when player click on question
     * @param col - is column position of character will change background color when player click on question
     */
    private void generatePuzzle(final int rowSize, int colSize, List<Integer> row, List<Integer> col, List<Integer> rowDisable, List<Integer> colDisable, boolean status) {
//        cells = new String[rowSize][colSize];
        jpnPuzzleInterface = new JPanel(new GridLayout(rowSize, colSize, 5, 5));
        wo = new JTextField[rowSize][colSize];
        //Loop for generate cell of puzzle
        for (int i = 0; i
                < rowSize; i++) {
            for (int j = 0; j
                    < colSize; j++) {
                wo[i][j] = new CrossFields(i, j);
                wo[i][j].setFont(new java.awt.Font("Arial", 1, 20));
                //Loop for change background of row or column contain question player choosing
                for (int k = 0; k
                        < row.size(); k++) {
                    for (int l = 0; l
                            < col.size(); l++) {
                        //Check position to change background color
                        if (i == row.get(k) && j == col.get(l)) {
                            wo[i][j].setBackground(new Color(10, 12, 15));
                        }
                    }
                }
                for (int k = 0; k
                        < rowDisable.size(); k++) {
                    for (int l = 0; l
                            < colDisable.size(); l++) {
                        //Check position to change background color
                        if (i == row.get(k) && j == col.get(l)) {
                            wo[i][j].setEnabled(false);
                        } else {
//                            System.out.println("Position: ("+i+", "+j+")");
                            wo[i][j].setEnabled(status);
                        }
                    }
                }

                wo[i][j].setEnabled(status);
                //Check if cells is not empty then disable cells

                if (cells != null && cells.length > 0) {
                    for (int k = 0; k
                            < cells.length; k++) {
                        for (int l = 0; l
                                < cells[k].length; l++) {
                            if (cells[k][l] != null && !cells[k][l].trim().equals("")) {
                                if (i == k && j == l) {
//                                    System.out.println("Position 1: ("+i+", "+j+")");
//                                    System.out.println("cell (" + k + ", " + l + ")" + cells[k][l]);
                                    wo[i][j].setEnabled(false);
                                    wo[i][j].setText(cells[k][l].toUpperCase());
                                }
                            }
                        }
                    }
                }
                //Set attribute for text box - Cell of Puzzle
                wo[i][j].setToolTipText("Lets enter a character");
                wo[i][j].setForeground(new Color(102, 0, 0));
                wo[i][j].setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
                wo[i][j].setHorizontalAlignment(SwingConstants.CENTER);
//                wo[i][j].setEnabled(status);

                wo[i][j].addMouseListener(new MouseAdapter() {

                    @Override
                    public void mousePressed(MouseEvent e) {
                        CrossFields cell = (CrossFields) e.getSource();

                        //Display virtual keyboard when righ mouse clicked
//                        if ((e.getModifiers() & InputEvent.BUTTON3_MASK) != 0) {
//                            DisplayKeyboard();
//                            Keyboard panel = new Keyboard();
//                            keypressed = panel.getKeyPressed();
//                            cell.setText(keypressed.toUpperCase());
//                        }
                        posX = cell.getPoX();
                        posY = cell.getPoY();
                        cell.setBackground(Color.green);
                    }
                }); //Add key listener for get character of palayer inputed
                wo[i][j].addKeyListener(new KeyAdapter() {

                    @Override
                    public void keyReleased(KeyEvent e) {
                        try {
                            CrossFields cell = (CrossFields) e.getSource();
//                            e.setKeyChar(String.valueOf(e.getKeyChar()).toUpperCase().charAt(0));
//                            String charinput = ((JTextField) e.getSource()).getText().substring(0, 1).toUpperCase();
//                            ((JTextField) e.getSource()).setText(charinput.toUpperCase());
                            JOptionPane.showMessageDialog(null, "Lets click choose position button");
                            posX = cell.getPoX();
                            posY = cell.getPoY();
//                            cells[posX][posY] = charinput;
                        } catch (Exception ex) {
//                            e.setKeyChar(e.getKeyChar());
                        }
                    }
                });
                jpnPuzzleInterface.add(wo[i][j]);
            }
        }

        jspnPuzzleInterface.setViewportView(jpnPuzzleInterface);
    }

    /**
     * DisplayKeyboard
     * @throws HeadlessException
     */
    public void DisplayKeyboard() throws HeadlessException {
        JDialog frame = new JDialog();
        Keyboard panel = new Keyboard();
//        frame.setContentPane(panel);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setResizable(false);
        frame.setModal(true);
        frame.setTitle("Cross-Words Virtual keyboard");
        frame.setForeground(Color.white);
        frame.setBackground(Color.lightGray);
        frame.getContentPane().add(panel, "Center");
//        frame.getContentPane().add(panel);
        panel.key();

        frame.pack();
        frame.setSize(300, 140);
        Point location = MouseInfo.getPointerInfo().getLocation();
        int x = (int) location.getX();
        int y = (int) location.getY();
        frame.setLocation(x, y);
        frame.setVisible(true);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.persistence.EntityManager CrossWordsAdminPUEntityManager;
    private javax.swing.JComboBox cboGameSize;
    private javax.swing.JComboBox cboLevel;
    private javax.swing.JComboBox cboSubject;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton jbtnAbout;
    private javax.swing.JButton jbtnAdminManagement;
    private javax.swing.JButton jbtnChoosePosition;
    private javax.swing.JButton jbtnCreatePuzzle;
    private javax.swing.JButton jbtnDeletePuzzle;
    private javax.swing.JButton jbtnExit;
    private javax.swing.JButton jbtnFinishPuzzle;
    private javax.swing.JButton jbtnHelp;
    private javax.swing.JButton jbtnHighScore;
    private javax.swing.JButton jbtnInsertQuestion;
    private javax.swing.JButton jbtnLevelManagement;
    private javax.swing.JButton jbtnLoadQuestion;
    private javax.swing.JButton jbtnLogout;
    private javax.swing.JButton jbtnResetPuzzle;
    private javax.swing.JButton jbtnServerManagement;
    private javax.swing.JButton jbtnShowDetaisOfPuzzle;
    private javax.swing.JButton jbtnShowQuestionOfPuzzle;
    private javax.swing.JButton jbtnSizeManagement;
    private javax.swing.JButton jbtnSubjectManagement;
    private javax.swing.JButton jbtnUpdatePuzzle;
    private javax.swing.JCheckBox jcbStatus;
    private javax.swing.JComboBox jcbxDirection;
    private javax.swing.JComboBox jcbxPuzzleName;
    private javax.swing.JLabel jlblCopyRigh;
    private javax.swing.JLabel jlblCreatedStatus;
    private javax.swing.JLabel jlblStatus;
    private javax.swing.JLabel jlblVisitWeb;
    private javax.swing.JPanel jpnPuzzle;
    private javax.swing.JPanel jpnPuzzleInterface;
    private javax.swing.JPanel jpnPuzzleManagement;
    private javax.swing.JScrollPane jspnPuzzleInterface;
    private javax.swing.JTable jtblPuzzleList;
    private javax.swing.JTabbedPane jtbpnPuzzleManagement;
    private javax.swing.JTextArea jtxaQuestion;
    private javax.swing.JTextField jtxtAnswer;
    private javax.swing.JTextField jtxtPuzzleName;
    private javax.swing.JLabel lblLastAccess;
    private javax.swing.JLabel lblWellcome;
    private javax.swing.JLabel lblbackground;
    private javax.swing.JPanel pnlMain;
    private java.util.List<cw.entity.Puzzle> puzzleList;
    private javax.persistence.Query puzzleQuery;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
